package com.aletto.general_store.ui.activities.orders;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.EditText;

import com.aletto.general_store.R;
import com.aletto.general_store.data.model.ConsumerOrder;
import com.aletto.general_store.data.model.ServiceOrder;

import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.ColorTransitionPagerTitleView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.viewpager2.widget.ViewPager2;

/**
 * 订单申诉
 */
public class AppealOrderActivity extends AppCompatActivity {

    private final List<AppealOrderFragment> fragmentList = new ArrayList<>(
            Arrays.asList(
                    AppealOrderFragment.newInstance(ServiceOrder.ONGOING), // 进行中
                    AppealOrderFragment.newInstance(ServiceOrder.COMPLETED) // 完成
            )
    );


    private EditText et_search;
    private Button button_search;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_consumer_order);
        findViewById(R.id.back_image).setOnClickListener(v -> finish());

        // 列表
        ViewPager2 view_pager = findViewById(R.id.view_pager);
        view_pager.setAdapter(new FragmentStateAdapter(this) {

            @NonNull
            @Override
            public Fragment createFragment(int position) {
                return fragmentList.get(position);
            }

            @Override
            public int getItemCount() {
                return fragmentList.size();
            }

        });

        initSearch(view_pager);

        MagicIndicator magicIndicator = findViewById(R.id.magic_indicator);
        CommonNavigator commonNavigator = getCommonNavigator(view_pager);
        magicIndicator.setNavigator(commonNavigator);
        view_pager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                magicIndicator.onPageScrolled(position, positionOffset, positionOffsetPixels);
            }

            @Override
            public void onPageSelected(int position) {
                magicIndicator.onPageSelected(position);
                button_search.performClick();
            }

            @Override
            public void onPageScrollStateChanged(int state) {
                magicIndicator.onPageScrollStateChanged(state);
            }
        });
    }


    @NonNull
    private CommonNavigator getCommonNavigator(ViewPager2 viewPager) {
        String[] tabsAuction = getResources().getStringArray(R.array.tabs_appeal_order);
        CommonNavigator commonNavigator = new CommonNavigator(this);
        commonNavigator.setAdjustMode(true);
        commonNavigator.setAdapter(new CommonNavigatorAdapter() {
            @Override
            public int getCount() {
                return tabsAuction.length;
            }

            @Override
            public IPagerTitleView getTitleView(Context context, int index) {
                ColorTransitionPagerTitleView colorTransitionPagerTitleView = new ColorTransitionPagerTitleView(context);
                colorTransitionPagerTitleView.setText(tabsAuction[index]);
                colorTransitionPagerTitleView.setGravity(Gravity.CENTER);
                colorTransitionPagerTitleView.setNormalColor(Color.parseColor("#848E9C"));
                colorTransitionPagerTitleView.setSelectedColor(Color.parseColor("#ED5E62"));
                colorTransitionPagerTitleView.setOnClickListener(v -> viewPager.setCurrentItem(index));
                return colorTransitionPagerTitleView;
            }

            @Override
            public IPagerIndicator getIndicator(Context context) {
                LinePagerIndicator linePagerIndicator = new LinePagerIndicator(context);
                linePagerIndicator.setMode(LinePagerIndicator.MODE_EXACTLY);
                linePagerIndicator.setColors(Color.parseColor("#ED5E62"));
                return linePagerIndicator;
            }
        });

        return commonNavigator;
    }

    private void initSearch(ViewPager2 viewPager) {
        et_search = findViewById(R.id.et_search);
        button_search = findViewById(R.id.button_search);

        button_search.setOnClickListener(v -> {
            Editable text = et_search.getText();
            int currentItem = viewPager.getCurrentItem();

            if (text == null || text.toString().isEmpty()) {
                fragmentList.get(currentItem).request(null);
                return;
            }

            fragmentList.get(currentItem).request(text.toString());
        });

        et_search.setOnEditorActionListener((v, actionId, event) -> {
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                button_search.performClick();
                return true;
            }

            return false;
        });

        et_search.addTextChangedListener(new TextWatcher() {

            @Override
            public void afterTextChanged(Editable s) {
                button_search.performClick();
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

        });
    }

}
